/*
 * Ejercicio realizado por Francisco Gonzalez-Armijo Riádigos
 * para la prueba de seleccion de http://www.nomasystems.es
 */

package es.kuassivi.android.cd.business;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import es.kuassivi.android.cd.CompanyDetailsActivity;
import es.kuassivi.android.cd.R;
import es.kuassivi.android.cd.adapters.CompanyAddressAdapter;
import es.kuassivi.android.cd.adapters.CompanyListAdapter;
import es.kuassivi.android.cd.adapters.CompanyPhoneAdapter;
import es.kuassivi.android.cd.beans.Address;
import es.kuassivi.android.cd.beans.Company;
import es.kuassivi.android.cd.beans.Phone;
import es.kuassivi.android.cd.tools.ImageFromUrl;

/**
 * Negocio para la carga de datos en vistas
 * 
 * @author kuassivi <a href="mailto:kuassivi@gmail.com">kuassivi@gmail.com</a>
 * @since 0.1
 */
public class ViewsBusiness implements OnItemClickListener
{
	
	private static final String locationFormat = "%s %s, (%s)";
	
	/**
	 * @var context Activity
	 */
	private static Activity context;

	/**
	 * @var List<Company> Array de las compañias obtenidas
	 */
	private static List<Company> aCompaniesList;

	/**
	 * @var Company
	 */
	private static Company oCompanyDetails; 
	
	
	/**
	 * 
	 * @param context
	 * @param response
	 * @throws IllegalStateException
	 * @throws IOException
	 * @throws JSONException
	 */
	
	static public void populateCompanies(Activity context, String response)
								throws IllegalStateException, IOException, JSONException 
	{
		ViewsBusiness.context = context;
		
		ListView mCompaniesListView = (ListView) context.findViewById(R.id.companyList);
        
        mCompaniesListView.setOnItemClickListener(new ViewsBusiness());
		
		JSONArray objs = new JSONArray(response);
		
		aCompaniesList = new ArrayList<Company>();
		
		JSONObject obj;
		
		for (int i = 0; i < objs.length(); i++) {
		
			obj = objs.getJSONObject(i);
			
			aCompaniesList.add(new Company( obj.getInt("id"), obj.getString("name") ) );
		
		}
		
		mCompaniesListView.setAdapter(new CompanyListAdapter(context, aCompaniesList));
	}
	
	/**
	 * Genera en la vista una conpañia y su informacion obtenidos por la peticion REST
	 * 
	 * @param context Context
	 * @param response String
	 * @throws IllegalStateException
	 * @throws IOException
	 * @throws JSONException
	 */
	static public void populateCompanyDetails(Activity context, String response)
								throws IllegalStateException, IOException, JSONException 
	{
		ViewsBusiness.context = context;
		
		List<Address> 	Address 	= new ArrayList<Address>();
		
		List<Phone> 	Phones 		= new ArrayList<Phone>();
		
		JSONObject obj = new JSONObject(response);

		JSONArray objsAddress = obj.getJSONArray("addressLines");
		
		for (int i = 0; i < objsAddress.length(); i++) {
			
			Address.add(new Address( (String) objsAddress.get(0) ) );
			
		}
		
		JSONArray objsPhones = obj.getJSONArray("phones");
		
		JSONObject objPhones;
		
		for (int i = 0; i < objsPhones.length(); i++) {
			
			objPhones = objsPhones.getJSONObject(i);
			
			Phones.add(new Phone(	objPhones.getString("countryCode"), 
									objPhones.getLong("subscriberNumber") ) );
			
		}
		
		oCompanyDetails = new Company(
											obj.getInt("id"),
											obj.getString("name"),
											obj.getString("logo"),
											obj.getString("website"),
											Address,
											obj.getInt("zipCode"),
											obj.getString("city"),
											obj.getString("country"),
											Phones
											);
		

        
		ImageView mCompanyImageView 		= (ImageView) context.findViewById(R.id.imgLogo);
        
		TextView mCompanyNameView 			= (TextView) context.findViewById(R.id.txt_company_name);
        
		TextView mCompanyWebsiteView 		= (TextView) context.findViewById(R.id.txt_website);
        
		TextView mCompanyLocationView 		= (TextView) context.findViewById(R.id.txt_location);
        
		ListView mCompanyAddressListView 	= (ListView) context.findViewById(R.id.listAdrressLines);
        
		ListView mCompanyPhonesListView 	= (ListView) context.findViewById(R.id.listPhones);
		
		
		
		Bitmap bm = ImageFromUrl.loadBitmap(oCompanyDetails.getLogo());

    	// populate
        mCompanyImageView.setImageBitmap(bm);
        
        mCompanyNameView.setText(oCompanyDetails.getName());
        
        mCompanyWebsiteView.setText(oCompanyDetails.getWebsite());
        
        String location = String.format(locationFormat, 
        									Integer.toString( oCompanyDetails.getZipCode() ),
        									oCompanyDetails.getCity(),
        									oCompanyDetails.getCountry() );
        
        mCompanyLocationView.setText( location );
        
        mCompanyAddressListView.setAdapter(
        				new CompanyAddressAdapter(context, oCompanyDetails.getAddressLines()));
        
        mCompanyPhonesListView.setAdapter(
        				new CompanyPhoneAdapter(context, oCompanyDetails.getPhones()));
		
	}
	

	@Override
	public void onItemClick(AdapterView<?> arg0, View item, int position, 
			long arg3) 
	{
		Intent i = new Intent(ViewsBusiness.context, CompanyDetailsActivity.class);
		
		Company company = aCompaniesList.get(position);
		
		i.putExtra("id", company.getId());
		
		// Lanzar CompanyDetailsActivity
		ViewsBusiness.context.startActivity(i);
	}
	
}
